package com.xiongkw;

import java.util.ArrayDeque;
import java.util.Queue;

public class TreeScan {
    Integer val;
    TreeScan left;
    TreeScan right;
    int mark;

    public TreeScan(Integer v){
        this.val = v;
    }
    public void add (Integer a){
        if(this.left == null){
            this.left = new TreeScan(a);
        }else if( this.right == null){
            this.right = new TreeScan(a);
        }else{
            if(mark == 0){
                this.left.add(a);
                mark = 1;
            }else{
                this.right.add(a);
                mark = 0;
            }
        }
    }

    public static void printPre(TreeScan tree){
        if(tree == null){
            return;
        }
        System.out.println(tree.val);
        printPre(tree.left);
        printPre(tree.right);
    }

    public static void printIn(TreeScan tree){
        if(tree == null){
            return;
        }
        printIn(tree.left);
        System.out.println(tree.val);
        printIn(tree.right);
    }

    public static void printPost(TreeScan tree){
        if(tree == null){
            return;
        }
        printIn(tree.left);
        printIn(tree.right);
        System.out.println(tree.val);

    }

    public static void printWidth(TreeScan tree){
        Queue<TreeScan> queue = new ArrayDeque<>();
        queue.add(tree);
        while (!queue.isEmpty()){
            TreeScan node = queue.remove();
            System.out.println(node.val);
            if(node.left != null){
                queue.add(node.left);
            }
            if(node.right != null){
                queue.add(node.right);
            }
        }
    }

    public static void printHeight(TreeScan tree){
        if(tree == null){
            return;
        }
        printIn(tree.left);
        printIn(tree.right);
        System.out.println(tree.val);

    }

    public static void main(String[] args) {
        TreeScan tree  = new TreeScan(1);
        tree.add(2);
        tree.add(3);
        tree.add(4);
        tree.add(5);
        tree.add(6);
        tree.add(7);

        System.out.println("------------");
        TreeScan.printPre(tree);
        System.out.println("------------");
        TreeScan.printIn(tree);
        System.out.println("------------");
        TreeScan.printPost(tree);
        System.out.println("------------");
        TreeScan.printWidth(tree);
    }

}
